//
// Created by QU on 24-6-27.
//

#include <iostream>
#include <string>

char unescaped_char(const char& ch)
{
    switch (ch)
    {
    case 'n': return '\n';
    case 'r': return '\r';
    case '0': return '\0';
    default: return ch;
    }
}


int main(int argc, char* argv[])
{
    std::string jsonString = R"JSON("str\n\"in"g")JSON";
    std::string target;

    enum { Raw, Escape } phase = Raw;

    for (auto i = 1; i < jsonString.size(); ++i)
    {
        ////////////////////////////
        /// If remove const next line, code hints work.
        ////////////////////////////
        const char& ch = jsonString[i];
        // string_view返回的是常量引用.
        if (phase == Raw)
        {
            if ('\\' == ch) { phase = Escape; }
            else if ('"' == ch) { break; }
            else { target += ch; }
        }
        else if (phase == Escape)
        {
            // 如果到了这里, 说明上一个字符是转义字符, phase的值变为Escaped
            target += unescaped_char(ch);
            phase = Raw;
        }
    }

    std::cout << "jsonString = " << jsonString << std::endl;
    std::cout << "target = " << target << std::endl;
}
